<?php
namespace app\controller\api;

use app\model\api\SkyActivity;
use app\model\api\SkyAncestor;
use app\model\api\SkyCategory;
use app\model\api\SkyConf;
use app\model\api\SkyDiyGoods;
use app\model\api\SkyGoods;
use app\model\api\SkyGuide;
use app\model\api\SkySeason;
use app\model\api\SkySeasonCandle;
use app\model\api\SkyStrategy;
use laytp\controller\Api;
use think\exception\ValidateException;
/**
 * Class Sky
 * @package app\index\controller
 */
class Sky extends Api
{
    protected $model;
    protected $category_model;

    protected function initialize()
    {
        $this->model = new SkyAncestor();
        $this->category_model = new SkyCategory();
    }

    /**
     * 图鉴导航
     * @return false|string|\think\response\Json
     */
    public function guide(){
        try{
            $category_guide = (new SkyGuide())->list();
            //分类导航
            $top_guide = array_shift($category_guide);
            //季节导航
            $bottom_guide = array_pop($category_guide);

            $list = (new SkyDiyGoods())->guide();
            array_unshift($list,array(
                'title' => '图鉴分类',
                'type' => 1,
                'category' => $top_guide
            ));
            array_push($list,array(
                'title' => '季节图鉴',
                'type' => 1,
                'category' => $bottom_guide
            ));
            return $this->success('success',$list);
        }catch (\Exception $exception){
            return $this->exceptionError($exception);
        }
    }

    /**
     * 图鉴列表
     * @return false|string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function good(){
        $data = input('post.');
        $category = !empty($data['category'])?$data['category']:'';

        try{
            $list = (new SkyGoods())->list($category);
            return $this->success('success',$list);

        }catch (ValidateException $exception){
            return $this->error($exception->getMessage(),-1);
        }
    }

    /**
     * @return false|string|\think\response\Json
     */
    public function tagList(){
        $data = input('post.');
        $category = !empty($data['category'])?$data['category']:'';

        try{
            //验证器
            validate(\app\validate\api\Sky::class)
                ->scene('tag')
                ->check([
                    'category' => $category,
                ]);

            $list = $this->category_model->list($category);
            return $this->success('success',$list);
        }catch (ValidateException $exception){
            return $this->error($exception->getMessage(),-1);
        }
    }

    /**
     * 先祖列表
     * @return false|string|\think\response\Json
     */
    public function list(){
        $data = input('post.');
        $category = !empty($data['category'])?$data['category']:'';
        $type = !empty($data['type'])?$data['type']:'';

        try{
            //验证器
            validate(\app\validate\api\Sky::class)
                ->scene('ancestor')
                ->check([
                    'category' => $category,
                    'type' => $type,
                ]);

            $list = $this->model->list($category,$type);
            return $this->success('success',[
                'list' => $list,
                'total' => $this->category_model->AncestorCost($type)
            ]);
        }catch (ValidateException $exception){
            return $this->error($exception->getMessage(),-1);
        }
    }

    /**
     * 先祖详情
     * @return false|string|\think\response\Json
     */
    public function detail(){
        $data = input('post.');
        $ancestor_id = !empty($data['ancestor_id'])?$data['ancestor_id']:'';
        try{
            //验证器
            validate(\app\validate\api\Sky::class)
                ->scene('detail')
                ->check([
                    'ancestor_id' => $ancestor_id
                ]);

            $detail = $this->model->detail($ancestor_id);
            return $this->success('success',$detail);
        }catch (ValidateException $exception){
            return $this->error($exception->getMessage(),-1);
        }
    }

    /**
     * 日历
     * @return false|string|\think\response\Json
     */
    public function calendar(){
        $data = input('post.');
        $date = !empty($data['date'])?$data['date']:date('Y-m-d');
        try{
            //验证器
            validate(\app\validate\api\Sky::class)
                ->scene('date')
                ->check([
                    'date' => $date
                ]);

            $detail = (new SkyActivity())->calendar($date);
            return $this->success('success',$detail);
        }catch (\Exception $e){
            return $this->exceptionError($e);
        }
    }


    /**
     * 季节
     * @return false|string|\think\response\Json
     */
    public function season(){
        try{
            $detail = (new SkySeason())->detail();
            return $this->success('success',$detail);
        }catch (\Exception $e){
            return $this->exceptionError($e);
        }
    }

    /**
     * 首页图片
     * @return false|string|\think\response\Json
     */
    public function getImages(){
        try{
            $list = (new SkyConf())->column("value","key");
            return $this->success('success',$list);
        }catch (\Exception $e){
            return $this->exceptionError($e);
        }
    }


    /**
     * 任务攻略
     * @return false|string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function strategy(){
        $data = input('post.');
        $season = !empty($data['season'])?$data['season']:'';
        try{
            //验证器
            validate(\app\validate\api\Sky::class)
                ->scene('season')
                ->check([
                    'season' => $season
                ]);

            $detail = (new SkyStrategy())->detail($season);
            return $this->success('success',$detail);
        }catch (ValidateException $exception){
            return $this->error($exception->getMessage(),-1);
        }
    }


    /**
     * 大蜡烛位置
     * @return false|string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function candle(){
        $data = input('post.');
        $season = !empty($data['season'])?$data['season']:'';
        try{
            //验证器
            validate(\app\validate\api\Sky::class)
                ->scene('season')
                ->check([
                    'season' => $season
                ]);

            $detail = (new SkySeasonCandle())->detail($season);
            return $this->success('success',$detail);
        }catch (ValidateException $exception){
            return $this->error($exception->getMessage(),-1);
        }
    }

}



